package com.example.backend.dto;

import com.example.backend.domain.enums.TicketPriority;
import com.example.backend.domain.enums.TicketStatus;
import com.example.backend.domain.enums.TicketSource;
import lombok.Data;
import org.springframework.format.annotation.DateTimeFormat;

import java.time.Instant;

/**
 * 工单筛选条件
 * 支持多维度筛选，优化查询性能
 */
@Data
public class TicketFilter {
    // 基础筛选条件
    private TicketStatus status;
    private TicketPriority priority;
    private TicketSource source;
    
    // 用户相关筛选
    private Long createdById;
    private Long assignedToId;
    private Long customerId;
    private Long categoryId;
    
    // 时间范围筛选
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Instant createdFrom;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Instant createdTo;
    
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Instant updatedFrom;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Instant updatedTo;
    
    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Instant dueFrom;

    @DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
    private Instant dueTo;
    
    // 文本搜索
    private String keyword;
    private String titleKeyword;
    private String descriptionKeyword;
    
    // 布尔条件筛选
    private Boolean isPublic;
    private Boolean isEscalated;
    private Boolean hasDueDate;
    
    // 数值范围筛选
    private Integer minSatisfactionScore;
    private Integer maxSatisfactionScore;
    
    // 排序相关
    private String sortBy = "createdAt";  // 默认按创建时间排序
    private String sortDirection = "DESC"; // 默认降序
    
    // 分页相关
    private Integer page = 0;
    private Integer size = 100;
    
    /**
     * 检查是否有有效的筛选条件
     */
    public boolean hasFilters() {
        return status != null || priority != null || source != null ||
               createdById != null || assignedToId != null || customerId != null ||
               categoryId != null || createdFrom != null || createdTo != null ||
               updatedFrom != null || updatedTo != null || dueFrom != null || dueTo != null ||
               keyword != null || titleKeyword != null || descriptionKeyword != null ||
               isPublic != null || isEscalated != null || hasDueDate != null ||
               minSatisfactionScore != null || maxSatisfactionScore != null;
    }
    
    /**
     * 检查是否有时间范围筛选
     */
    public boolean hasTimeRange() {
        return createdFrom != null || createdTo != null || 
               updatedFrom != null || updatedTo != null ||
               dueFrom != null || dueTo != null;
    }
    
    /**
     * 检查是否有文本搜索
     */
    public boolean hasTextSearch() {
        return keyword != null || titleKeyword != null || descriptionKeyword != null;
    }
}


